import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/my/team_focus_entity.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:ball_lion/entity/team/lq_team_detail_entity.dart';
import 'package:ball_lion/server/api/basketball_api.dart';
import 'package:ball_lion/module/team/basketball/lq_team_count_view.dart';
import 'package:ball_lion/module/team/basketball/lq_team_info_view.dart';
import 'package:ball_lion/module/team/basketball/lq_team_lineup_view.dart';
import 'package:ball_lion/module/team/basketball/lq_team_schedule_view.dart';

import 'package:ball_lion/server/api/user_api.dart';
import 'package:ball_lion/utils/toast_utils.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';

class LqTeamDetailLogic extends GetxController
    with GetTickerProviderStateMixin {
  ScrollController scrollC = ApmScrollController();
  final _opacity = (-1.0).obs;
  int teamId = Get.arguments;
  LqTeamDetailEntity? data;
  List<String> tabs = [];
  List pages = [];
  bool isFocus = false;
  late TabController tabController;
  final GlobalKey<ExtendedNestedScrollViewState> scrollKey =
      GlobalKey<ExtendedNestedScrollViewState>();

  double get opacity {
    if (_opacity < 0) {
      return 0.0;
    } else if (_opacity > 1) {
      return 1.0;
    } else {
      return _opacity.value;
    }
  }

  @override
  void onInit() async {
    scrollC.addListener(() {
      double offset = scrollC.offset;
      _opacity.value = (offset - 120) / 28;
    });
    super.onInit();
  }

  @override
  void onReady() async {
    super.onReady();
    await getData();
  }

  Future getData() async {
    final result = await BasketballApi.getlqTeamDetail(teamId);
    if (result != null) {
      data = result;
      if (data?.national == 1) {
        tabs = ['资料', '赛程', '阵容'];
        pages = [
          LqTeamInfoView(),
          LqTeamScheduleView(),
          LqTeamLineupView(),
        ];
      } else {
        tabs = ['资料', '赛程', '统计', '阵容'];
        pages = [
          LqTeamInfoView(),
          LqTeamScheduleView(),
          LqTeamCountView(),
          LqTeamLineupView(),
        ];
      }
      tabController =
          TabController(length: tabs.length, vsync: this, initialIndex: 1);
      getFocus();
      update();
    }
  }

  Future getFocus() async {
    if (!User.isLogin) return;
    List<TeamFocusEntity>? list = await UserApi.getTeamFocusList(sportsId: 2);
    isFocus = list.any((e) => e.id == teamId);
    update();
  }

  onFocus() async {
    int? result;
    await getFocus();
    if (isFocus) {
      result = await UserApi.unFocusTeam(teamId, 2);
      if (result == 200) {
        Toast.text("取消关注成功");
        getData();
      } else {
        Toast.text("取消关注失败");
      }
    } else {
      Utils.onEvent('sjpd_qdxq', params: {'sjpd_qdxq': '3'});
      result = await UserApi.focusTeam(teamId, 2);
      if (result == 200) {
        Toast.text("关注成功");
        getData();
      }
    }
  }
}
